//
//  BRCrossingTableViewController.m
//  百思不得姐
//
//  Created by LinBrong on 16/8/26.
//  Copyright © 2016年 LinBrong. All rights reserved.
//

#import "BRCrossingTableViewController.h"
#import "UIImageView+WebCache.h"
#import "BRCrossingTableViewCell.h"
#import "MJRefreshAutoStateFooter.h"
#import "MJRefreshStateHeader.h"
#import "MJRefresh.h"
#import "NetWorkTool.h"
#import "HomeViewController.h"
#import "commentViewController.h"


static NSString *const BRCrossingTableViewCellID =  @"BRCrossingTableViewCellID";
@interface BRCrossingTableViewController ()<BRCrossingTableViewCellDelegate>


/*  <#    #>  */
@property (strong, nonatomic) NSMutableArray *dataArr;


@end

@implementation BRCrossingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];

    // 马上进入刷新状态(三方框架上下拉刷新)
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        
        [self loadMoreData];
    }];
    
//    //右边刷新
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"cellFollowClickIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(loadNewData)];
    
    self.tableView.estimatedSectionHeaderHeight = 20;
    _dataArr = [NSMutableArray array];
    //        self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = [UIColor grayColor];
    //设置UITableView的预设行高
    self.tableView.estimatedRowHeight = 1000;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BRCrossingTableViewCell" bundle:nil] forCellReuseIdentifier:BRCrossingTableViewCellID];
}


#pragma mark -tableView的数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.dataArr.count;
    
    
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BRCrossingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BRCrossingTableViewCellID];
    
    //当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
    if(cell == nil){
        while ([cell.contentView.subviews lastObject] != nil) {
            [(BRCrossingTableViewCell *)[cell.contentView.subviews lastObject] removeFromSuperview];
            
            
        }
    }
    cell.delegate = self;
    cell.model = self.dataArr[indexPath.row];
    return cell;
    
}




//连接地址一来就下载
- (void)setURLStr:(NSString *)URLStr {
    _URLStr = URLStr;
    
//    NSLog(@"tableView %@",URLStr);
    
    //    if(self.dataArr == nil){
    [self loadNewData];
    //    }else {
    //        [self.tableView reloadData];
    //    }
    
    //        [BRCrossingModel downloadWithURLStr:URLStr successBlock:^(NSArray *Array) {
    //            [self.tableView reloadData];
    //            [self.dataArr addObjectsFromArray:Array];
    ////                    self.dataArr = Array;
    //            BRCrossingModel *model = self.dataArr[0];
    //
    //            NSLog(@"-+-%@",model.profile_image);
    //
    //            [self.tableView reloadData];
    //
    //        } failBlock:^(NSError *error) {
    //            NSLog(@"%@",error);
    //        }];
    
}

- (void)loadNewData {
    
    NSMutableString *URLStr = [NSMutableString stringWithString:self.URLStr];
    
    [BRCrossingModel downloadWithURLStr:URLStr successBlock:^(NSArray *Array) {
        //        [self.tableView reloadData];
        [self.dataArr removeAllObjects];
        [self.dataArr addObjectsFromArray:Array];
        
//        NSLog(@"%zd",self.dataArr.count);
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)loadMoreData {
    
    NSMutableString *URLStr = [NSMutableString stringWithString:self.URLStr];
    
    
    [BRCrossingModel downloadWithURLStr:URLStr successBlock:^(NSArray *Array) {
        [self.tableView reloadData];
        
        [self.dataArr addObjectsFromArray:Array];
        
        
//        NSLog(@"%zd",self.dataArr.count);
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
    } failBlock:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - cell的代理方法
- (void)loadPersonalPageDataWithUid:(NSString *)uid
{
    
    HomeViewController *personalVc = [HomeViewController new];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:personalVc];
    
    
    personalVc.uid = uid;

    
    [self.navigationController pushViewController:navc animated:YES];
//    [self presentViewController:personalVc animated:YES completion:nil];
}

@end
